package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.service.MenuService;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @Reference
    private PermissionService permissionService;

    @GetMapping("/page")
    public Result findByPage(QueryPageBean queryPageBean){
        try {
            if(queryPageBean!=null){
                PageResult pageResult = menuService.findByPage(queryPageBean);
                return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, pageResult);
            }else{
                PageResult pageResult=menuService.findAll();
                return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, pageResult);           }
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    //新增菜单
    @PostMapping
    public Result addMenu(@RequestBody Menu menu,@RequestParam("permissionIds") Integer[] permissionIds){
        try {
            menuService.insert(menu,permissionIds);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }


    }
    //修改菜单
    @PutMapping
    public Result updateMenu(@RequestBody Menu menu,
                             @RequestParam("permissionIds") Integer[] permissionIds){
        try {
            menuService.update(menu,permissionIds);
            return new Result(true,MessageConstant.UPDATE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_MENU_FAIL);
        }
    }
    /*删除菜单*/
    @DeleteMapping("/{id}")
    public Result deleteMenu(@PathVariable Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }
    //根据菜单查询权限信息
    @GetMapping("/{id}")
    public Result findPermissions(@PathVariable Integer id){
        Menu menu = menuService.findOneById(id);
        if (id!=null){
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        }
        return new Result(false,MessageConstant.QUERY_MENU_FAIL);
    }
    //查询所有权限
    @GetMapping("/permission/all")
    public Result findAllPermissions(){
        List<Permission> allPermission = permissionService.findAllPermission();
        if (allPermission!=null){
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,allPermission);
        }
        return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
    }

}
